package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.model.entity.BcUserBankcards;
import com.cloud.bc.model.entity.BcWechausers;
import com.cloud.bc.model.entity.BcWithdrawalOrders;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.*;
import com.cloud.common.security.AESUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/account/withdrawal")
public class Ajax_WithdrawalController {
    private final BcUsersService bcUsersService;
    private final BcUserBankcardsService bcUserBankcardsService;
    private final Config config;
    private final BcWechausersService bcWechausersService;
    private final BcWithdrawalOrdersService bcWithdrawalOrdersService;

    /**
     * 绑卡
     */
    @PostMapping("bind")
    public AjaxResp bind(HttpServletRequest request,
                         HttpServletResponse response,
                         BcUserBankcards req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        String id = AESUtil.decrypt(Convert.toStr(req.getNumber(), ""), config.getOpenidSecret());
        BcWechausers bcWechausers = bcWechausersService.getById(id);
        if (null == bcWechausers) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("账户错误，请后退重新绑定");
            return ajaxResp;
        }
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        req.setUserId(userInfo.getId());
        req.setNumber(bcWechausers.getOpenid());
        BcUserBankcards bcUserBankcards = new BcUserBankcards();
        bcUserBankcards.setUserId(userInfo.getId());
        bcUserBankcards.setWay(req.getWay());
        BcUserBankcards bcUserBankcardsCheck = bcUserBankcardsService.selectOne(bcUserBankcards);
        if (null != bcUserBankcardsCheck) {
            //存在就更新
            req.setId(bcUserBankcardsCheck.getId());
            bcUserBankcardsService.updateById(req);
        } else {
            //不存在就创建
            bcUserBankcardsService.save(req);
        }
        return ajaxResp;
    }

    /**
     * 提现申请
     */
    @PostMapping("apply")
    public AjaxResp apply(HttpServletRequest request,
                          HttpServletResponse response,
                          BcWithdrawalOrders req
    ) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        req.setUserId(userInfo.getId());
        Integer status = bcWithdrawalOrdersService.apply(req);
        if (status == -1) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("余额不够,提现失败");
        } else if (status == -2) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("提现额度错误，提现失败");
        } else if (status == -3) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("请先绑定账户信息");
        }
        return ajaxResp;
    }
}
